﻿Public Class prooncontact
    Private POCID = Nothing
    Private PROID = Nothing
    Private MTRGTID = Nothing
    Private STRGTID = Nothing
    Private SBRGTID = Nothing
    Private STATUS = Nothing
    Private selectProoncontact = "SELECT a.`pocid` AS 'รหัสการผูกโปรโมชั่น', a.`proid` AS 'รหัสโปรโมชั่น', e.`proname` AS 'ชื่อโปรโมชั่น', a.`mtrgtid` AS 'รหัสหมวดคู่สัญญา', d.`mtrgtname` AS 'ชื่อหมวดคู่สัญญา', a.`strgtid` AS 'รหัสกลุ่มคู่สัญญา', c.`strgtname` AS 'ชื่อกลุ่มคู่สัญญา', a.`sbrgtid` AS 'รหัสสิทธิ์', b.`subrgtname` AS 'ชื่อสิทธิ์', a.`status` AS 'สถานะการเปิดใช้งาน' FROM prooncontact a LEFT JOIN massubrgt b ON a.sbrgtid = b.SBRGTID LEFT JOIN masstypergt c ON a.strgtid = c.strgtid LEFT JOIN masmtypergt d ON a.mtrgtid = d.mtrgtid JOIN maspromotion e ON a.proid = e.proid"
    'Private selectMasdrgacc = "SELECT DGTYID, DRGTYPE, convert(bit, [STATUS]) FROM MASDRGTYPE"

    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Property POCID_ As Integer
        Get
            Return POCID
        End Get
        Set(value As Integer)
            POCID = value
        End Set
    End Property

    Property PROID_ As Integer
        Get
            Return PROID
        End Get
        Set(value As Integer)
            PROID = value
        End Set
    End Property

    Property MTRGTID_ As Integer
        Get
            Return MTRGTID
        End Get
        Set(value As Integer)
            MTRGTID = value
        End Set
    End Property

    Property STRGTID_ As Integer
        Get
            Return STRGTID
        End Get
        Set(value As Integer)
            STRGTID = value
        End Set
    End Property

    Property SBRGTID_ As Integer
        Get
            Return SBRGTID
        End Get
        Set(value As Integer)
            SBRGTID = value
        End Set
    End Property

    Property STATUS_ As Boolean
        Get
            Return STATUS
        End Get
        Set(value As Boolean)
            If value = True Then
                STATUS = 1
            Else
                STATUS = 0
            End If
        End Set
    End Property

    Property selectProoncontact_ As String
        Get
            Return selectProoncontact
        End Get
        Set(value As String)

        End Set
    End Property

    Public Sub mtrgtcombobox(ByVal comboboxName As ComboBox)
        Dim mtrgtData As DataTable
        mtrgtData = connect.GetTable("SELECT mtrgtid, mtrgtname FROM masmtypergt WHERE status = '1'")
        Try
            With comboboxName
                .DataSource = mtrgtData
                .DisplayMember = "MTRGTNAME"
                .ValueMember = "MTRGTID"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub strgtcombobox(mtrgt As String, ByVal comboboxName As ComboBox)
        Dim strgtData As DataTable
        strgtData = connect.GetTable("SELECT strgtid, strgtname FROM masstypergt WHERE mtrgtid = '" & mtrgt & "' AND status = '1'")
        Try
            With comboboxName
                .DataSource = strgtData
                .DisplayMember = "strgtname"
                .ValueMember = "strgtid"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub sbrgtcombobox(strgt As String, ByVal comboboxName As ComboBox)
        Dim sbrgtData As DataTable
        sbrgtData = connect.GetTable("SELECT sbrgtid, subrgtname from massubrgt where strgtid = '" & strgt & "' AND status = '1'")
        Try
            With comboboxName
                .DataSource = sbrgtData
                .DisplayMember = "subrgtname"
                .ValueMember = "sbrgtid"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub ProidCombobox(ByVal comboboxName As ComboBox)
        Dim proidData As DataTable
        proidData = connect.GetTable("SELECT proid, proname FROM maspromotion WHERE status = '1'")
        Try
            With comboboxName
                .DataSource = proidData
                .DisplayMember = "proname"
                .ValueMember = "proid"
                '.BindingContext = BindingContext
            End With
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Public Sub insertProoncontactData()
        If connect.GetTable("SELECT pocid FROM prooncontact WHERE sbrgtid = '" & SBRGTID & "' AND proid = '" & PROID & "'").Rows.Count > 0 Then
            MsgBox("มีข้อมูลการผูกโปรโมชั่นนี้อยู่ในระบบแล้ว")
        Else
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()
                'Dim dischargeID As Integer

                Dim queryString As String
                queryString = "INSERT INTO prooncontact (proid, mtrgtid, strgtid, sbrgtid, status) VALUES ('" & PROID & "', "

                If MTRGTID = 0 Then
                    queryString += "null, "
                Else
                    queryString += "'" & MTRGTID & "', "
                End If

                If STRGTID = 0 Then
                    queryString += "null, "
                Else
                    queryString += "'" & STRGTID & "', "
                End If

                If SBRGTID = 0 Then
                    queryString += "null, "
                Else
                    queryString += "'" & SBRGTID & "', "
                End If

                queryString += "'" & STATUS & "')"

                connect.ExecuteNonQuery(queryString)
                'connect.ExecuteNonQuery("INSERT INTO prooncontact (PROID, MTRGTID, STRGTID, SBRGTID, STATUS) VALUES ('" & PROID & "', '" & MTRGTID & "', '" & STRGTID & "', '" & SBRGTID & "', '" & STATUS & "')")
                'dischargeID = connect.ExecuteScalar("INSERT INTO masdischarge (DISCHARGE, STATUS) VALUES ('" & masdischargeDescription & "', '" & masdischargeStatus & "'); SELECT LAST_INSERT_ID();")
                'connectlocal.ExecuteNonQuery("INSERT INTO MASDISCHARGE (DISCHGID, DISCHARGE, STATUS) VALUES ('" & dischargeID & "', '" & masdischargeDescription & "', '" & masdischargeStatus & "')")
                MsgBox("ท่านได้ทำการผูกข้อมูลโปรโมชั่นเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        End If
    End Sub

    Public Sub editProoncontactData()
        If TypeOf PROID Is Integer Then
            Try
                connect = CONDB.NewConnection()
                'connectlocal = conlocal.NewConnection()

                connect.ExecuteNonQuery("UPDATE prooncontact SET proid = '" & PROID & "', mtrgtid = '" & MTRGTID & "' , strgtid = '" & STRGTID & "' , sbrgtid = '" & SBRGTID & "', status = '" & STATUS & "' WHERE pocid = '" & POCID & "'")
                'connectlocal.ExecuteNonQuery("UPDATE MASDISCHARGE SET DISCHARGE = '" & masdischargeDescription & "', STATUS = '" & masdischargeStatus & "' WHERE DISCHGID = '" & masdischargeID & "'")
                MsgBox("ท่านได้ทำการแก้ไขข้อมูลวิธีการเก็บยาเรียบร้อยแล้ว")
            Catch ex As Exception
                MsgBox(vbCrLf & ex.Message)
            End Try
            connect.Dispose()
            'connectlocal.Dispose()
        Else
            MsgBox("โปรดกรุณาระบุข้อมูลให้ถูกต้อง ต้องเป็นตัวอักษร")
        End If
    End Sub
End Class
